package controllers;

import java.util.List;

import play.Logger;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;

import models.Oeuvre;

public class SparqlRequests{
	
	
	public String parametre;
	
	
	public SparqlRequests(String parametre){
		this.parametre=parametre;
	}
	
	public String getParametre(){
		return this.parametre;
	}
	
	public void setParametre(String parametre){
		this.parametre=parametre;		
	}
	
	@SuppressWarnings("null")
	public static  List<Oeuvre> ExecuteRequete(String param){
		
		Model model = ModelFactory.createDefaultModel();	
		
		String queryString = 
				  "PREFIX dc: <http://purl.org/dc/elements/1.1/>"
				+ "PREFIX edm: <http://www.europeana.eu/schemas/edm/>"
	          + "SELECT DISTINCT ?auteur ?type ?date{"
	          + "?a dc:title \"" + param + "\" ."
	          + "?a dc:creator   ?auteur ."
	          + "?a edm:type     ?type ."
	          + "?a dc:date      ?date ."
	          + "}";
		Query query = QueryFactory.create(queryString) ;
	    QueryExecution qexec = QueryExecutionFactory.create(query, model);
	    //QueryExecutionFactory.sparqlService("http://europeana.ontotext.com/sparql", query);
	    try{
	    	ResultSet results = qexec.execSelect() ;
	    	List<Oeuvre> res=null;
		    for(;results.hasNext();){
		    		
		    		QuerySolution soln = results.nextSolution();
		    		RDFNode auteur	=soln.get("?auteur");
		          //  l.Auteur=auteur.toString();
		            RDFNode type=soln.get("?type");
		           // l.Type=type.toString();
		    		RDFNode date=soln.get("?date");
		            //l.Date=date.toString();
		            Oeuvre l=new Oeuvre();
		            res.add(l);	       
		            
		    }
		    return res;
		}
	    finally{
	    	    qexec.close();
	    }
		

	}
}
